package com.sentineldemo.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author luowenjie
 * @since 2025/4/25 17:32
 */
@RestController
public class TestController {

    @GetMapping("/test")
    @SentinelResource(value = "testResource", blockHandler = "handleBlock")
    public String test() {

        // 模拟一些业务逻辑，可能会调用其他服务或执行耗时操作
        // ...
        return "Hello, Sentinel!";
    }

    public String handleBlock(BlockException ex) {

        // 处理熔断降级的逻辑
        // 这里可以返回错误信息、备用数据或者执行其他降级策略
        return "Blocked by Sentinel: " + ex.getClass().getSimpleName();
    }

}
